package com.backend.service.Impl;

import com.backend.common.AddAss;
import com.backend.common.AdminForm;
import com.backend.common.GetAllUserContion;
import com.backend.common.UserPassword;
import com.backend.config.Result;
import com.backend.mapper.AdminMapper;
import com.backend.service.AdminService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl  implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public Result lookUpAllUser() {
        List<Map<String,String>> stringMap=adminMapper.lookUpAllUser();

        return Result.success("查找成功",stringMap);
    }

    @Override
    public Result updateAdminPassword(UserPassword userPassword) {
        if(!userPassword.getNewPassword().equals(userPassword.getNewPassword1())){
            return Result.fail("两次输入的新密码不一致",0);
        }
        Map<String,String> map=adminMapper.getAssAdmin(userPassword.getUsername());
        if(!map.get("admin_password").equals(userPassword.getOldPassword())){
            return Result.fail("旧密码输入错误",0);
        }
        Integer i=adminMapper.updateAdminPassword(userPassword);
        return Result.success("修改成功",1);
    }

    @Override
    public Result AuditActionStatus(String actionId, String status) {
        Integer i=adminMapper.AuditActionStatus(actionId,status);
        return Result.success("查找成功",i);
    }

    @Override
    public Result addAss(AddAss addAss) {
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        addAss.setAssDate(sdf.format(date));

        Map<String,String> map=adminMapper.getAssAdmin(addAss.getTopUser());
        if(map==null){
            //        注册社团
            Integer i=adminMapper.addAss(addAss);
            //进行标号赋予用户权限
            List<Map<String,String>> list=adminMapper.getAllAss();
            //添加新的社团管理员
            Integer t=adminMapper.insertAssAdmin(addAss.getTopUser(),String.valueOf(list.get(list.size()-1).get("id")));
            return Result.success("注册成功",t);
        }

        return Result.success("注册失败，该负责人已有社团",0);
    }

    @Override
    public Result deleteAss(String assId) {
//        删除社团信息;
        Integer i=adminMapper.deleteAssById(assId);
//        删除社团管理员权限
        Integer j=adminMapper.deleteAdminByAssId(assId);
        return Result.success("注销成功",i+j);
    }

    @Override
    public Result adminLogin(AdminForm adminForm) {
        Map<String,String> stringMap=adminMapper.adminLogin(adminForm);
        return Result.success("登录成功",stringMap);
    }

    @Override
    public Result getAssUserByAId(GetAllUserContion getAllUserContion) {

        int i=1;
        List<Map<String,String>> mapList=adminMapper.getAssUserByAssId(getAllUserContion);

        for (Map<String,String> map:mapList) {
            map.put("id",i+++"");
        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result getAssUserByAssId(GetAllUserContion getAllUserContion) {

        PageHelper.startPage(getAllUserContion.getPageNum(),getAllUserContion.getPageSize());
        int i=1;
        List<Map<String,String>> mapList=adminMapper.getAssUserByAssId(getAllUserContion);
        if(getAllUserContion.getPageNum()!=1){
            i=getAllUserContion.getPageSize()+i;
        }

        for (Map<String,String> map:mapList) {
            map.put("id",i+++"");
        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result getAssAllUserByAssId(GetAllUserContion getAllUserContion) {
        List<Map<String,String>> mapList=adminMapper.getAssUserByAssId(getAllUserContion);
        int i=1;
        if(getAllUserContion.getPageNum()!=1){
            i=getAllUserContion.getPageSize()+i;
        }

        for (Map<String,String> map:mapList) {
            map.put("id",i+++"");
        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Integer AssDeleteUserById(String stuId, String assId) {
        return adminMapper.AssDeleteUserById(stuId,assId);
    }

    @Override
    public Integer AssUpdateUserDef(String stuId, String assId,String status) {
        return adminMapper.AssUpdateUserDef(stuId,assId,status);
    }

    public static void main(String[] args) {
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
}
